//
//  LogonViewController.m
//  EggSteward
//
//  Created by 王道林 on 16/8/27.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "LogonViewController.h"
#import "Masonry.h"
#import "BaseLabel.h"
#import "BaseButton.h"
#import "BaseTextField.h"
#import "DLWNetworkingParser.h"
#import "ZhengZe.h"
#import "RetrieveViewController.h"
#import "RegisterViewController.h"
#import "SetViewController.h"
#import "BaseNavigationController.h"
@interface LogonViewController ()<UITextFieldDelegate, UITabBarDelegate>
@property (nonatomic, strong) UIImageView *imageview;
@property (nonatomic, strong) BaseTextField *textFieldPhone;
@property (nonatomic, strong) BaseTextField *textfieldPwd;
@property (nonatomic, strong) UIView *viewPhone;
@property (nonatomic, strong) UIView *viewPwd;
@property (nonatomic, strong) BaseButton *buttonAtuo;
@property (nonatomic, strong) BaseButton *buttonBackPwd;
@property (nonatomic, strong) BaseButton *buttonLogon;
@property (nonatomic, strong) BaseButton *buttonRegister;

@end

@implementation LogonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self createSubview];
    [self addGesture];
    [self setAttribute];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
}

#pragma mark - 设置属性
- (void)setAttribute
{
    // 设置title 的颜色.属性
    NSDictionary *attributes=[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil];
    self.navigationItem.title = @"用户登录";
    [self.navigationController.navigationBar setTitleTextAttributes:attributes];
    // bar的背景色
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.145 green:0.715 blue:0.292 alpha:1.000];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage = [UIImage new];
    
    self.tabBarController.tabBar.hidden = YES;

}
#pragma mark -创建视图
- (void)createSubview
{
    // 背景图片
    _imageview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loginBack"]];
    [self.view addSubview:_imageview];
    [_imageview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view);
        make.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.view.mas_top);
        make.height.mas_equalTo(self.view.mas_height).multipliedBy(0.28);
    }];
    
    // 手机号的textField
    _textFieldPhone = [[BaseTextField alloc] init];
    [self.view addSubview:_textFieldPhone];
    _textFieldPhone.keyboardType = UIKeyboardTypeNumberPad;
    _textFieldPhone.borderStyle = UITextBorderStyleNone;
    _textFieldPhone.placeholder = @"请输入手机号";
    _textFieldPhone.delegate = self;
    [_textFieldPhone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_imageview.mas_bottom).with.offset(40);
        make.left.mas_equalTo(self.view.mas_left).with.offset(40);
        make.right.mas_equalTo(self.view.mas_right).with.offset(-40);
        make.height.mas_equalTo(50);
    }];
    //电话号下面的横条
    _viewPhone = [[UIView alloc] init];
    [self.view addSubview:_viewPhone];
    _viewPhone.backgroundColor = [UIColor grayColor];
    [_viewPhone mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_textFieldPhone.mas_bottom).with.offset(0);
        make.left.mas_equalTo(self.view.mas_left).with.offset(40);
        make.right.mas_equalTo(self.view.mas_right).with.offset(-40);
        make.height.mas_equalTo(1);
    }];
    
    // 密码
    _textfieldPwd = [[BaseTextField alloc] init];
    [self.view addSubview:_textfieldPwd];
    _textfieldPwd.borderStyle = UITextBorderStyleNone;
    _textfieldPwd.placeholder = @"请输入密码";
    _textfieldPwd.delegate = self;
    // 密文输入
    _textfieldPwd.secureTextEntry = YES;
    [_textfieldPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_textFieldPhone.mas_bottom).with.offset(40);
        make.left.mas_equalTo(self.view.mas_left).with.offset(40);
        make.right.mas_equalTo(self.view.mas_right).with.offset(-40);
        make.height.mas_equalTo(50);
    }];
    //密码下面的横条
    _viewPwd = [[UIView alloc] init];
    [self.view addSubview:_viewPwd];
    _viewPwd.backgroundColor = [UIColor grayColor];
    [_viewPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_textfieldPwd.mas_bottom).with.offset(0);
        make.left.mas_equalTo(self.view.mas_left).with.offset(40);
        make.right.mas_equalTo(self.view.mas_right).with.offset(-40);
        make.height.mas_equalTo(1);
    }];
    
    //自动登录label
    BaseLabel *labelAtuo = [[BaseLabel alloc] init];
    [self.view addSubview:labelAtuo];
    labelAtuo.text = @"自动登录";
    labelAtuo.textColor = [UIColor grayColor];
    labelAtuo.backgroundColor = [UIColor whiteColor];
    [labelAtuo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_viewPwd.mas_bottom).with.offset(40);
        make.left.mas_equalTo(self.view.mas_left).with.offset(40);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.25);
        make.height.mas_equalTo(30);
    }];
    
    // 找回密码
    _buttonBackPwd = [BaseButton buttonWithType:UIButtonTypeSystem];
    [_buttonBackPwd setTitle:@"找回密码" forState:UIControlStateNormal];
    _buttonBackPwd.titleLabel.font = [UIFont systemFontOfSize:18];
    [_buttonBackPwd setTintColor:[UIColor grayColor]];
    [self.view addSubview:_buttonBackPwd];
    [_buttonBackPwd addTarget:self action:@selector(backpwdAction:) forControlEvents:UIControlEventTouchUpInside];
    [_buttonBackPwd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_viewPwd.mas_bottom).with.offset(40);
        make.right.mas_equalTo(self.view.mas_right).with.offset(-40);
        make.width.mas_equalTo(self.view.mas_width).multipliedBy(0.25);
        make.height.mas_equalTo(30);
    }];
    
    // 登录按钮
    _buttonLogon = [BaseButton buttonWithType:UIButtonTypeCustom];
    [_buttonLogon setTitle:@"登录" forState:UIControlStateNormal];
    // 切圆角
    _buttonLogon.layer.masksToBounds = YES;
    _buttonLogon.layer.cornerRadius = 25;
    _buttonLogon.titleLabel.font = [UIFont systemFontOfSize:25];
    _buttonLogon.backgroundColor = [UIColor colorWithRed:0.157 green:0.651 blue:0.278 alpha:1.000];
    // button的点击方法
    [_buttonLogon addTarget:self action:@selector(logon:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_buttonLogon];
    [_buttonLogon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_buttonBackPwd.mas_bottom).with.mas_offset(30);
        make.left.mas_equalTo(self.view.mas_left).with.mas_offset(70);
        make.right.mas_equalTo(self.view.mas_right).with.mas_offset(-70);
        make.height.mas_equalTo(50);
    }];
//    我要注册新账号的button
    _buttonRegister = [BaseButton buttonWithType:UIButtonTypeSystem];
    [_buttonRegister setTitle:@"我要注册新账号" forState:UIControlStateNormal];
    _buttonRegister.titleLabel.font = [UIFont systemFontOfSize:20];
    [_buttonRegister setTintColor:[UIColor colorWithRed:0.157 green:0.651 blue:0.278 alpha:1.000]];
    [_buttonRegister addTarget:self action:@selector(registerAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_buttonRegister];
    [_buttonRegister mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_buttonLogon.mas_bottom).with.mas_offset(40);
        make.left.mas_equalTo(self.view.mas_left).with.mas_offset(70);
        make.right.mas_equalTo(self.view.mas_right).with.mas_offset(-70);
        make.height.mas_equalTo(30);
    }];

    
}

#pragma mark - textfield协议方法
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
// 绿条变色
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if (textField == _textFieldPhone) {
        _viewPhone.backgroundColor = [UIColor colorWithRed:0.157 green:0.651 blue:0.278 alpha:1.000];
    } else
    {
        _viewPwd.backgroundColor = [UIColor colorWithRed:0.157 green:0.651 blue:0.278 alpha:1.000];
    }
}
// 颜色变回来
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField == _textFieldPhone) {
        _viewPhone.backgroundColor = [UIColor grayColor];
    } else
    {
        _viewPwd.backgroundColor = [UIColor grayColor];
    }

}


#pragma mark - 登录
- (void)logon:(BaseButton *)button
{
    if (![ZhengZe isMobile:_textFieldPhone.text]) {
        //提示框
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"消息通知" message:@"请输入账号" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
        [alertController addAction:actionCancel];
        [self presentViewController:alertController animated:YES completion:^{
            
        }];
    } else if ([ZhengZe isMobile:_textFieldPhone.text] && [_textfieldPwd.text isEqualToString:@""])
    {
        //提示框
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"消息通知" message:@"请输入密码" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }];
        [alertController addAction:actionCancel];
        [self presentViewController:alertController animated:YES completion:^{
            
        }];
    }
    else
    {
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:_textFieldPhone.text, @"user_name", _textfieldPwd.text, @"user_pwd", nil];
    [DLWNetworkingParser POSTWithURL:@"http://api.danjiguanjia.com/v1/User/login" par:dic success:^(id responseObject) {

        NSString *stringMessage = responseObject[@"msg"];
       if ([stringMessage isEqualToString:@"登录成功"]) {
           //登录成功
           
           NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
            [userDefault setValue:responseObject[@"data"][@"ticket"] forKey:@"ticket"];
            [userDefault setValue:responseObject[@"data"][@"uid"] forKey:@"uid"];
           if ([_setlogon isEqualToString:@"setlogon"])
           {
               SetViewController *setVC = [[SetViewController alloc] init];
               NSArray *arr = @[setVC];
               self.navigationController.viewControllers = arr;
               [self.navigationController pushViewController:setVC animated:YES];
           } else
           {
              BaseNavigationController *naviVC = [self.tabBarController.viewControllers lastObject];
               SetViewController *setVC = [[SetViewController alloc] init];
               NSArray *arr = @[setVC];
               naviVC.viewControllers = arr;
           [self.navigationController popToRootViewControllerAnimated:YES];
           }
        } else
        {
            // 提示框
            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"消息通知" message:stringMessage preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *actionCancel = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
                [self dismissViewControllerAnimated:YES completion:nil];
            }];
            [alertController addAction:actionCancel];
            [self presentViewController:alertController animated:YES completion:^{
                
            }];

        }
        
        
    } filed:^(NSError *error) {
        
    }];
    }
}

#pragma mark - 推出注册界面

- (void)registerAction:(BaseButton *)button
{
    RegisterViewController *registerVC = [[RegisterViewController alloc] init];
    [self.navigationController pushViewController:registerVC animated:YES];
}


#pragma mark - 推出找回密码界面
- (void)backpwdAction:(BaseButton *)button
{
    RetrieveViewController *retrieveVC = [[RetrieveViewController alloc] init];
    [self.navigationController pushViewController:retrieveVC animated:YES];
}

#pragma mark -添加下滑手势,回收键盘
- (void)addGesture
{
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
   
    [self.view addGestureRecognizer:tapGesture];
}
-(void)tapAction:(UITapGestureRecognizer *)tap
{
    [_textfieldPwd resignFirstResponder];
    [_textFieldPhone resignFirstResponder];
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
